package hydrap2p.manager;
import org.apache.thrift.transport.TSocket;


public class TimeoutChecker extends Thread
{
	private TSocket socket;
	private long lasttime;
	private final int INTERVAL=2000;
	private final int TIMEOUT=5000;
	private boolean running;
	
	public TimeoutChecker()
	{
		socket=null;
		running=true;
	}
	
	@Override
	public void run()
	{
		while(running)
		{
			try {
				Thread.sleep(INTERVAL);
			} catch (InterruptedException e) {
				// Interrupted
			}
			if (running && socket!=null)
			{
				if (System.currentTimeMillis()>lasttime+TIMEOUT)
				{
					socket.close();
				}
			}
		}
	}
	
	public void setCurrentConnection(TSocket socket)
	{
		this.socket=socket;
		lasttime=System.currentTimeMillis();
	}
	public void finishedCurrentConnect()
	{
		this.socket=null;
	}
	
	public void shutdown()
	{
		running=false;
		this.interrupt();
	}
	
}
	